﻿using System;
using System.Data;
using Entities;
using System.Data.SqlClient;
using System.Collections.Generic;


namespace DataAccess
{
    public class CoachTypeDAL : BaseObject
    {
        #region Public Methods
        ///<summary>
        ///Create Obj from ireader.
        ///</summary>

        private CoachType GetObjectFromReader(IDataReader reader)
        {
            CoachType temp = new CoachType();
            temp.CoachTypeID = (reader["CoachTypeID"] is DBNull) ? int.MinValue : (System.Int32)reader["CoachTypeID"];
            temp.CoachTypeName = (reader["CoachTypeName"] is DBNull) ? string.Empty : (System.String)reader["CoachTypeName"];
            temp.RatioFare = (reader["RatioFare"] is DBNull) ? 0 : (float)reader["RatioFare"];

            return temp;
        }

        ///<summary>
        ///Get CoachTypeDAL by id 
        ///</summary>
        public CoachType Get_CoachType_By_ID(int CoachTypeID)
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand("", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@CoachTypeID", CoachTypeID));
                conn.Open();
                CoachType obj = new CoachType();
                using (IDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    while (reader.Read())
                    {
                        obj = GetObjectFromReader(reader);
                    }
                }
                conn.Close();
                return obj;
            }
        }

        ///<summary>
        ///Get List CoachTypeDAL as list 
        ///</summary>
        /// <param name="whereSql">Where fields. example : 1=1 </param>
        /// <param name="orderBy"Order fields></param>
        public List<CoachType> GetList_CoachType(string whereSql, string orderBy)
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand("", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@WhereCondition", whereSql));
                cmd.Parameters.Add(new SqlParameter("@OrderByExpression", orderBy));
                SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
                try
                {
                    conn.Open();
                    List<CoachType> lst = new List<CoachType>();
                    using (IDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        while (reader.Read())
                        {
                            lst.Add(GetObjectFromReader(reader));
                        }
                    };
                    return lst;
                }
                catch
                {
                    return null;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                        conn.Close();
                }
            }
        }

        ///<summary>
        ///Get List CoachTypeDAL as list 
        ///</summary>
        /// <param name="whereSql">Where fields. example : 1=1 </param>
        /// <param name="orderBy"Order fields></param>
        public List<CoachType> GetAll_CoachType()
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand("", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
                try
                {
                    conn.Open();
                    List<CoachType> lst = new List<CoachType>();
                    using (IDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        while (reader.Read())
                        {
                            lst.Add(GetObjectFromReader(reader));
                        }
                    };
                    return lst;
                }
                catch
                {
                    return null;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                        conn.Close();
                }
            }
        }

        ///<summary>
        ///Using for paging data 
        ///</summary>
        /// <param name="pageIndex">Number of pages</param>
        /// <param name="PageSize">Number of a rows per page</param>
        /// <param name="conditions">Where  fields</param>
        /// <param name="groupBy">Group by fields</param>
        public DataTable Get_CoachTypeAsPaging(int pageIndex, int pageSize, string sSortColumn, string sFields, string sWhere, string sGroupBy)
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand("", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
                cmd.Parameters.Add(new SqlParameter("@PageSize", pageSize));
                cmd.Parameters.Add(new SqlParameter("@SortColumn", sSortColumn));
                cmd.Parameters.Add(new SqlParameter("@Fields", sFields));
                cmd.Parameters.Add(new SqlParameter("@Where", sWhere));
                cmd.Parameters.Add(new SqlParameter("@Group", sGroupBy));
                SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
                try
                {
                    conn.Open();
                    DataTable tbl = new DataTable();
                    dataAdapter.Fill(tbl);
                    conn.Close();
                    return tbl;
                }
                catch
                {
                    return null;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                        conn.Close();
                }
            }
        }

        ///<summary>
        ///Insert new CoachTypeDAL
        ///</summary>
        public void Insert_CoachType(CoachType temp)
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand("", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@CoachTypeID", temp.CoachTypeID));
                cmd.Parameters.Add(new SqlParameter("@Password", temp.CoachTypeName));
                cmd.Parameters.Add(new SqlParameter("@Name", temp.RatioFare));

                try
                {
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
                catch (SqlException ex)
                {
                    throw ex;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                        conn.Close();
                }
            }
        }

        ///<summary>
        ///Update a CoachTypeDAL
        ///</summary>
        public void Update_CoachType(CoachType temp)
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand("", conn);
                cmd.Parameters.Add(new SqlParameter("@CoachTypeID", temp.CoachTypeID));
                cmd.Parameters.Add(new SqlParameter("@CoachTypeName", temp.CoachTypeName));
                cmd.Parameters.Add(new SqlParameter("@RatioFare", temp.RatioFare));
                try
                {
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
                catch (SqlException ex)
                {
                    throw ex;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                        conn.Close();
                }
            }
        }

        ///<summary>
        ///Delete a CoachTypeDAL
        ///</summary>
        public void Delete_CoachType(CoachType temp)
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand("", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@CoachTypeID", temp.CoachTypeID));
                try
                {
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
                catch (SqlException ex)
                {
                    throw ex;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                        conn.Close();
                }
            }
        }

        #endregion
    }
}
